package com.twosnail.frame.commin.util.message;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import org.json.JSONException;
import org.json.JSONObject;

import javax.ws.rs.core.MediaType;

/**
 * Created by yangcs on 2017/7/20.
 */
public class SendMessage {
    public static void main(String[] args) {
        SendMessage api = new SendMessage();
        String httpResponse =  api.send("15823895646","123456");
        try {
            JSONObject jsonObj = new JSONObject( httpResponse );
            int error_code = jsonObj.getInt("error");
            String error_msg = jsonObj.getString("msg");
            if(error_code==0){
                System.out.println("Send message success.");
            }else{
                System.out.println("Send message failed,code is "+error_code+",msg is "+error_msg);
            }
        } catch (JSONException ex) {

        }
    }

    public static String send(String mobile,String code){
        String message = "感谢您购买Bitness运动管家健身次卡，您健身次卡权益激活码为"+code+"，该激活码只能使用一次，请妥善保管。【比特律动】";
        return sendMessage(mobile,code,message);
    }

    public static String send1(String mobile,String code){
        String message = "感谢您购买Bitness智能健康心帖，您Bitness健身管家公众号的VIP激活码为"+code+"，该激活码只能使用一次，请妥善保管。【比特律动】";
        return sendMessage(mobile,code,message);
    }

    private static String sendMessage(String mobile,String code,String message){
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
                "api","key-54c9934f61ef9d5fc94b28a3e7b1ae3d"));
        WebResource webResource = client.resource(
                "http://sms-api.luosimao.com/v1/send.json");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        formData.add("mobile", mobile);
        formData.add("message", message);
        ClientResponse response =  webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).
                post(ClientResponse.class, formData);
        String textEntity = response.getEntity(String.class);
        return textEntity;
    }
}
